namespace ObjectPersistence {
    using System;
    using System.Text;
    using System.Collections;
    using System.Data.Common;
    using System.Transactions;
    using System.Collections.Generic;
    
    public partial class CommentAssembler : AssemblerBaseUpdatable<KYO.DAL.Comment, CommentCriteria, CommentRowMap, CommentDomainObjectFactory> {
        public CommentAssembler() {
            base.connection = new System.Data.SqlClient.SqlConnection(GlobalUtil.Constants.ConnectionString);
        }
        protected override System.Data.Common.DbConnection InstanceConnection {
            get {
                return this.connection;
            }
        }
        /// <summary>Reads a single object from the database corresponding to the object keys which are passed in</summary>
        public KYO.DAL.Comment ReadSingleByKey(int CommentId) {
            CommentCriteria criteria = new CommentCriteria();
            criteria.Set_CommentId(CommentCriteria.AVAILABLE_OPERATORS.EQUALS, CommentId);
            return this.ReadSingle(criteria);
        }

        internal override void MakePersistent(KYO.DAL.Comment PersistentObject, bool cascade, System.Data.Common.DbCommand comm) {
            if ((PersistentObject.IsNew == false)) {
                if ((GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Contains(PersistentObject.ROKeyString) 
                            && (PersistentObject.ChangesMade == false))) {
                    return;
                }
                if ((GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Contains(PersistentObject.ROKeyString) == false)) {
                    GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Add(PersistentObject.ROKeyString);
                }
            }
            if (cascade) {
                InformationAssembler tempInformationAssembler = new InformationAssembler();

                if ((PersistentObject.IsInformationObjectNull() == false)) {
                    comm.Parameters.Clear();
                    tempInformationAssembler.MakePersistent(PersistentObject.InformationObject, cascade, comm);
                    if (((PersistentObject.InformationId == PersistentObject.InformationObject.InformationID) 
                                == false)) {
                        PersistentObject.InformationId = PersistentObject.InformationObject.InformationID;
                    }
                }
                tempInformationAssembler.Dispose();
            }
            if ((PersistentObject.IsNew || PersistentObject.ChangesMade)) {
                CommentUpdateFactory updateFactory = new CommentUpdateFactory();
                comm.Parameters.Clear();
                if ((PersistentObject.IsNew == false)) {
                    updateFactory.updateCommandText(PersistentObject, comm);
                    int numRows = comm.ExecuteNonQuery();
                    if ((numRows == 0)) {
                        throw new NoRowsUpdated();
                    }
                }
                else {
                    updateFactory.insertCommandText(PersistentObject, comm);
                    comm.CommandText = (comm.CommandText + "; SELECT SCOPE_IDENTITY();");
                    decimal NewIdentityKey = ((decimal)(comm.ExecuteScalar()));
                    PersistentObject.CommentId = ((int)(NewIdentityKey));
                    GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Add(PersistentObject.ROKeyString);
                    PersistentObject.IsNew = false;
                }
            }
            PersistentObject.ChangesMade = false;

        }

        internal override void Delete(KYO.DAL.Comment PersistentObject, System.Data.Common.DbCommand comm) {
            comm.Parameters.Clear();

            CommentSelectionFactory tempCommentSelectionFactory = new CommentSelectionFactory();
            string sql = "DELETE FROM [dbo].[Comment]";
            comm.CommandText = sql;
            tempCommentSelectionFactory.getDefaultSelection(PersistentObject, comm);
            comm.ExecuteNonQuery();
        }

        #region Manage Child Objects / Collections
        /// Reads the KYO.DAL.Information from the database corresponding to the object keys passed as parameters
        // <param name="localInformationId">Corresponds to Comment attribute InformationId</param
        public KYO.DAL.Information GetInformationObject(int localInformationId) {
            KYO.DAL.Information result = new KYO.DAL.Information();
            InformationCriteria InformationCriteriaSingle = new InformationCriteria();
            InformationCriteriaSingle.Set_InformationID(InformationCriteria.AVAILABLE_OPERATORS.EQUALS, localInformationId);
            InformationAssembler _tempAssem = new InformationAssembler();
            result = _tempAssem.ReadSingle(InformationCriteriaSingle);
            return result;
        }
        #endregion

        #region Helper Utilities
        protected override CommentRowMap fillRowMap(System.Data.Common.DbDataReader reader) {
            CommentRowMap result = new CommentRowMap();
            result.CommentId = reader.GetInt32(reader.GetOrdinal("dboCommentDOTCommentId"));
            result.InformationId = reader.GetInt32(reader.GetOrdinal("dboCommentDOTInformationId"));
            result.Email = reader.GetString(reader.GetOrdinal("dboCommentDOTEmail"));
            result.Name = reader.GetString(reader.GetOrdinal("dboCommentDOTName"));
            result.Content = reader.GetString(reader.GetOrdinal("dboCommentDOTContent"));
            if ((reader.IsDBNull(reader.GetOrdinal("dboCommentDOTDateCreate")) == false)) {
                result.DateCreate = reader.GetDateTime(reader.GetOrdinal("dboCommentDOTDateCreate"));
            }
            return result;
        }
        internal override void ClearList() {
            GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Clear();
        }

        public override string Abstract_SELECT {
            get {
                return ("SELECT " + CommentAssembler.RawFields);
            }
        }
        protected override string InstanceTable {
            get {
                return "[dbo].[Comment]";
            }
        }
        public const string RawFields = @"[dbo].[Comment].[CommentId] AS dboCommentDOTCommentId, [dbo].[Comment].[Content] AS dboCommentDOTContent, [dbo].[Comment].[DateCreate] AS dboCommentDOTDateCreate, [dbo].[Comment].[Email] AS dboCommentDOTEmail, [dbo].[Comment].[InformationId] AS dboCommentDOTInformationId, [dbo].[Comment].[Name] AS dboCommentDOTName";
        public const string SELECT = @"SELECT [dbo].[Comment].[CommentId] AS dboCommentDOTCommentId, [dbo].[Comment].[Content] AS dboCommentDOTContent, [dbo].[Comment].[DateCreate] AS dboCommentDOTDateCreate, [dbo].[Comment].[Email] AS dboCommentDOTEmail, [dbo].[Comment].[InformationId] AS dboCommentDOTInformationId, [dbo].[Comment].[Name] AS dboCommentDOTName";
        #endregion
    }
}
